package com.changgou.controller;

import com.changgou.file.FastDFSFile;
import com.changgou.util.FastDFSUtils;
import entity.Result;
import entity.StatusCode;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/upload")
@CrossOrigin
public class FileUploadController {

    @PostMapping
    public Result upload(@RequestParam(value = "file")MultipartFile file) throws Exception {
        // 封装文件信息
        FastDFSFile fastDFSFile = new FastDFSFile(
                file.getOriginalFilename(), // 文件名 1.jpg
                file.getBytes(),  //文件字节数组
                StringUtils.getFilenameExtension(file.getOriginalFilename()) //获取文件扩展名
        );

        String[] upLoad = FastDFSUtils.upLoad(fastDFSFile);
        // 拼接访问地址
        String url = FastDFSUtils.getTrackerInfo()+"/"+upLoad[0]+"/"+upLoad[1];
        return new Result(true, StatusCode.OK,"文件上传成功",url);
    }


}
